using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Dungeon : SerializedMonoBehaviour
{
    public Dictionary<GameObject, int> prefabs;

    public AlertBehaviour alert;

    public List<Transform> summonPoint;

    public int monsterNum;
    private int targetNum;
    private int killNum;

    private List<MonsterController> monsters = new List<MonsterController>();

    private PlayerCharacterController player;

    private bool isSummon;

    private float _time;
    private int _allWeight;

    private void Start()
    {
        targetNum = monsterNum;
    }
    private void Update()
    {
        if (alert.listTarget.Count > 0)
        {
            for (int i = 0; i < alert.listTarget.Count; i++)
            {
                PlayerCharacterController play = alert.listTarget[i].GetComponent<PlayerCharacterController>();
                if (play != null)
                {
                    player = play;
                    break;
                }
                else player = null;
            }
        }
        else player = null;

        if (monsters.Count > 0)
        {
            for (int i = 0; i < monsters.Count; i++)
            {
                if (monsters[i].hp <= 0)
                {
                    killNum++;
                    monsters.Remove(monsters[i]);
                }
            }
        }

        if (player == null)
        {
            if (monsters.Count > 0)
            {
                for (int i = 0; i < monsters.Count; i++)
                {
                    monsters[i].target = null;
                }
            }
            return;
        }
        else 
        {
            if (monsters.Count > 0)
            {
                for (int i = 0; i < monsters.Count; i++)
                {
                    if (monsters[i].target == null && monsters[i].hp > 0)
                    {
                        monsters[i].target = player;
                    }
                }
            }
        }

        if (killNum == targetNum)
        {
            GameData.PassLevel();
            GameData.AddMissionPrograss(3, "", 1);
            gameObject.SetActive(false);
        }

        isSummon = monsters.Count >= 3 ? false : true;

        if (isSummon)
        {
            if (monsterNum == 0)
            {
                return;
            }

            _time += Time.deltaTime;

            if (_time >= 5)
            {
                GameObject prefab = GetPrefab();
                Transform born = GetBorn();
                GameObject obj = Instantiate(prefab);
                MonsterController monster = obj.GetComponent<MonsterController>();
                monster.GetComponent<NavMeshAgent>().Warp(born.position);
                monster.type = MonsterController.MonsterType.Elite;
                monster.target = player;
                monsters.Add(monster);
                monsterNum--;
                _time = 0;
            }
        }
        else _time = 0;
    }

    private Transform GetBorn()
    {
        int v = Random.Range(0, summonPoint.Count);

        return summonPoint[v];
    }

    private GameObject GetPrefab()
    {
        if (_allWeight == -1)
        {
            _allWeight = 0;
            foreach (var item in prefabs)
            {
                _allWeight += item.Value;
            }
        }

        int v = Random.Range(0, _allWeight);

        foreach (var item in prefabs)
        {
            if (v < item.Value)
            {
                return item.Key;
            }
        }
        return null;
    }
}
